package org.fmi.lje.modules;

import java.util.HashMap;
import java.util.Map;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

import com.google.inject.Provider;

public class SvgGraphicsProvider implements Provider<SVGGraphics2D> {
	
	public static final Map<SVGGraphics2D, SVGDocument> cache = new HashMap<SVGGraphics2D, SVGDocument>();

	@Override
	public SVGGraphics2D get() {
		// Create an SVG document.
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);

        // Create a converter for this document.
        SVGGraphics2D svgGraphics2D = new SVGGraphics2D(doc);
        cache.put(svgGraphics2D, doc);
        
		return svgGraphics2D;
	}

	public static Element getDocumentElement(SVGGraphics2D svgGraphics2D) {
		return cache.get(svgGraphics2D).getDocumentElement();
	}
}
